﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Project;

using Jzy.Core.Common.Web;
using System.Collections.Generic;

using Jzy.Model.Project;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Enums;
using Frame.Lib.Core.Log;
using Frame.Lib.Core.Log.Operate;
using Jzy.DAL;

public partial class Project_Task_List : PageList
{
    private PrjTaskBO _task;
    private PrjStatus _prjStatus;
    private int PrjID
    {
        get
        {
            return int.Parse(StrUtil.DecryptUrl(Request["PrjID"]));
        }
    }

    /// <summary>
    /// 判断是某一个用户的项目还是所有项目
    /// </summary>
    private int? UserID
    {
        get
        {
            if (string.IsNullOrEmpty(Request["UserID"]))
            {
                return null;
            }
            else
            {
                return int.Parse(Decrypt(Request["UserID"]));
            }
        }
    }

    //判断是否是归档项目或作废项目
    private int? Status
    {
        get
        {
            if (string.IsNullOrEmpty(Request["Status"]))
            {
                return null;
            }
            else
            {
                return int.Parse(Decrypt(Request["Status"]));
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        _task = new PrjTaskBO(PrjID);
        _prjStatus = (PrjStatus)(new ProjectBO()).GetInfo(PrjID).Status;
        if (!IsPostBack)
        {
            BindView();
            ProjectBO prj = new ProjectBO();
            prj.BindPrj(drpPrj, Status, UserID);
            drpPrj.SelectedValue = PrjID.ToString();
        }

        AddAttr();
        base.InitSelectID(hidSelectIDs);

        hidSelectIDs.Value = strSelectIDs;
        lnkReturn.HRef = "../List.aspx";
        JSUtil.SetTableHeight("list", 70);
    }
    private void AddAttr()
    {
        JSUtil.DelConfirm(btnDelete, "任务");
        //btnMove.Attributes.Add("onclick", "return moveDir('../../Common/Dialog/MoveTree.aspx?TableName=" + (int)TableName.PrjTask + "&','" + hidSelectIDs.ClientID + "'," + btnRefresh.ClientID + ")");
        JSUtil.ClickBtn(lnkDelete, btnDelete);
        JSUtil.ClickBtn(lnkMoveLeft, btnMoveLeft);
        JSUtil.ClickBtn(lnkMoveRight, btnMoveRight);
        JSUtil.ClickBtn(lnkMoveUp, btnMoveUp);
        JSUtil.ClickBtn(lnkMoveDown, btnMoveDown);

        btnMoveLeft.Attributes.Add("onclick", "return MoveCheck()");
        btnMoveRight.Attributes.Add("onclick", "return MoveCheck()"); 
        btnMoveUp.Attributes.Add("onclick", "return MoveCheck()");
        btnMoveDown.Attributes.Add("onclick", "return MoveCheck()");
        //JSUtil.ClickBtn(lnkMove, btnMove);
       
    }
    private void BindView()
    {
        LVUtil.Init(lv, lvHeader);

        List<PrjTaskInfo> list = GetData();

        lv.DataSource = list;
        lv.DataKeyNames = new string[] { "ID", "UpperID" ,"HasChild","IsMilestone" };
        lv.DataBind();
        
        if (IsPostBack)
        {
            LVUtil.RestoreTreeListState(lv, hidHideTrIDs.Value);
        }

        JSUtil.ExcuteJS("InitSelect();");

    }

    private List<PrjTaskInfo> GetData()
    {
        int rootID = (int)_task.GetRootID();
        List<PrjTaskInfo> list = _task.GetTreeList(rootID);
        return list;
    }
    protected void drpPrj_SelectedIndexChanged(object sender, EventArgs e)
    {
        string url = "List.aspx?PrjID=" + Encrypt(drpPrj.SelectedValue);
        if (UserID != null)
        {
            url += "&UserID=" + Encrypt(UserID);
        }
        if (Status != null)
        {
            url += "&Status=" + Encrypt(Status);
        }
        Response.Redirect(url, true);
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindView();
    }
    protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            LVUtil.SetEdit(sender, e, 410, 390, GlobalVar.WebCurrPath + "Edit.aspx?PrjID=" + PrjID);
            HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
            HyperLink lnkEdit = (HyperLink)e.Item.FindControl("lnkEdit");

            lnkName.Attributes.Remove("onclick");
            HtmlTableRow row = null;
            foreach (Control ctl in e.Item.Controls)
            {
                if (ctl is HtmlTableRow)
                {
                    row = (HtmlTableRow)ctl;
                    break;
                }
            }
            string strID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString();
            row.Attributes["id"] = strID;
            string upperID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["UpperID"].ToString();
            row.Attributes.Add("UpperID", upperID);

            string strUrl = GlobalVar.WebCurrPath + "Edit.aspx?PrjID=" + PrjID + "&UpperID=" + StrUtil.EncryptUrl(strID);
            HyperLink lnkAdd = (HyperLink)e.Item.FindControl("lnkAdd");
            JSUtil.ShowForm(lnkAdd, "增加子任务", null, strUrl, 410, 340);

            bool hasChild = (bool)lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["HasChild"];
            if (hasChild)
            {
                row.Style.Add("font-weight", "bold");       
            }
           

            CheckBox chkSelect = (CheckBox)e.Item.FindControl("chkSelect");
            if (upperID == Constants.TREE_ROOT_UPPER_ID.ToString())
            {
                chkSelect.Enabled = false;
                //lnkName.Enabled = false;
                //lnkName.Attributes.Remove("onclick");
                lnkEdit.Enabled = false;                
                lnkName.NavigateUrl = "../TabList.aspx?PrjID=" + StrUtil.EncryptUrl(PrjID.ToString());

            }
            else
            {
                lnkName.NavigateUrl = "TabList.aspx?PrjID=" + StrUtil.EncryptUrl(PrjID.ToString()) + "&TaskID=" + StrUtil.EncryptUrl(strID);
            }

            HyperLink lnkSchedule = (HyperLink)e.Item.FindControl("lnkSchedule");
            strUrl = GlobalVar.WebAppPath + "Project/Schedule/Edit.aspx?PrjID=" + PrjID + "&TaskID=" + strID;
            JSUtil.ShowForm(lnkSchedule, "添加最新进展", null, strUrl, 370, 291);

            HyperLink lnkOversee = (HyperLink)e.Item.FindControl("lnkOversee");
            strUrl = GlobalVar.WebAppPath + "Project/Oversee/Edit.aspx?PrjID=" + PrjID + "&TaskID=" + strID;
            JSUtil.ShowForm(lnkOversee, "添加督办", null, strUrl, 360, 221);

            HyperLink lnkAttach = (HyperLink)e.Item.FindControl("lnkAttach");
            strUrl = GlobalVar.WebAppPath + "Project/Attach/Add.aspx?PrjID=" + PrjID + "&TaskID=" + strID;
            JSUtil.ShowForm(lnkAttach, "上传附件", null, strUrl, 360, 143);


            if (_prjStatus != PrjStatus.启动)
            {
                chkSelect.Enabled = lnkEdit.Enabled = lnkAdd.Enabled = lnkSchedule.Enabled = lnkOversee.Enabled = lnkAttach.Enabled = false;
            }


            bool isMilestone = (bool)lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["IsMilestone"];
            if (isMilestone)
            {
                row.Style.Add("color", "red");
                lnkAdd.Style.Add("color", "red");
                lnkEdit.Style.Add("color", "red");
                lnkName.Style.Add("color", "red");
                lnkSchedule.Style.Add("color", "red");
                lnkAttach.Style.Add("color", "red");
                lnkOversee.Style.Add("color", "red");
            }
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (strSelectIDs == "")
        {
            return;
        }

        List<int> ids = StrUtil.SplitToInt(strSelectIDs);
        ProjectInfo prjInfo = (new ProjectBO()).GetInfo(PrjID);

        foreach (int id in ids)
        {
            PrjTaskInfo info = _task.GetInfo(id);

            Logger.Operate(OperateType.Delete, OTableName.PrjTask, "删除了项目[" + prjInfo.Name + "]中的任务[" + info.Name + "]");


        }

        _task.Delete(ids.ToArray());
        hidSelectIDs.Value = "";
        BindView();
    }
    
    protected void btnMoveLeft_Click(object sender, EventArgs e)
    {
        List<int> ids = StrUtil.SplitToInt(strSelectIDs);
        List<PrjTaskInfo> list = GetData();
        _task.MoveLeft(ids, list);
        BindView();
    }
    protected void btnMoveRight_Click(object sender, EventArgs e)
    {
        List<int> ids = StrUtil.SplitToInt(strSelectIDs);
        List<PrjTaskInfo> list = GetData();
        _task.MoveRight(ids, list);
        BindView();
    }
    protected void btnMoveUp_Click(object sender, EventArgs e)
    {
        List<int> ids = StrUtil.SplitToInt(strSelectIDs);
        List<PrjTaskInfo> list = GetData();
        _task.MoveUp(ids, list);
        BindView();
    }
    protected void btnMoveDown_Click(object sender, EventArgs e)
    {
        List<int> ids = StrUtil.SplitToInt(strSelectIDs);
        List<PrjTaskInfo> list = GetData();
        _task.MoveDown(ids, list);
        BindView();
    }
}
